package controller;

import dao.BookAdminDao;
import dao.BookDao;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.Book;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

// TODO:待完成的后台功能
@WebServlet(name = "AdminBookServlet", value = "/admin/AdminBook")
public class AdminBookServlet extends HttpServlet {
    public static final Logger logger = LogManager.getLogger(AdminBookServlet.class);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method") == null ? "" : request.getParameter("method");
        RequestDispatcher requestDispatcher;
        ServletContext servletContext = this.getServletContext();
        BookDao bookDao = new BookDao();
        BookAdminDao bookAdminDao = new BookAdminDao();
        // 搜索图书
        if ("search".equals(method)) {
            Book book = (Book) request.getAttribute("search");
            try {
                ArrayList<Book> bookList = bookDao.getBook(book);
                logger.info("searchBookList date:" + bookList);
                request.setAttribute("bookList", bookList);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/deal/book_list.jsp");
            requestDispatcher.forward(request, response);
        }

        if ("bookList".equals(method)) {
            try {
                ArrayList<Book> bookArrayList = bookDao.getBookList();
                logger.info("getBookList date:" + bookArrayList);
                request.setAttribute("bookList", bookArrayList);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/deal/book_list.jsp");
            requestDispatcher.forward(request, response);
        }

        if ("addBook".equals(method)) {
            Book book = (Book) request.getAttribute("addBook");
            if (bookAdminDao.addBook(book)) {
                request.getSession().setAttribute("addBook_message", "图书添加成功");
                response.sendRedirect("/admin/book_add.jsp");
            } else {
                request.getSession().setAttribute("addBook_message", "图书添加失败");
                response.sendRedirect("/admin/book_add.jsp");
            }
        }

        if ("delBook".equals(method)) {
            int bookId = Integer.parseInt(request.getParameter("id"));
            logger.info("delBook by id = " + bookId);
            if (bookAdminDao.delBook(bookId)) {
                logger.info("delBook by id = " + bookId + " success!");
                request.getSession().setAttribute("delBook_message", "图书删除成功");
            } else {
                logger.info("delBook by id = " + bookId + " failed!");
                request.getSession().setAttribute("delBook_message", "图书删除失败");
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/book_del.jsp");
            requestDispatcher.forward(request, response);
        }

        if ("modBook".equals(method)) {
            Book book = (Book) request.getAttribute("modBook");
            try {
                if (bookAdminDao.modBook(book)) {
                    request.getSession().setAttribute("modBook_message", "图书修改成功");
                    response.sendRedirect("/admin/book_list.jsp");
                } else {
                    request.getSession().setAttribute("modBook_message", "图书修改失败");
                    response.sendRedirect("/admin/book_list.jsp");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if ("getBook".equals(method)) {
            int bookId = Integer.parseInt(request.getParameter("id"));
            logger.info("getBook by id = " + bookId);
            try {
                ArrayList<Book> bookInfo = bookDao.getBookInfo(bookId);
                logger.info("getBookInfo by id date:" + bookInfo);
                request.setAttribute("bookList", bookInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/book_info.jsp");
            requestDispatcher.forward(request, response);
        }
    }
}
